﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CMS;
using System.Linq;
using System.Data.SqlClient;
using System.Configuration;

public partial class Hours_view : System.Web.UI.Page
{
    public CMS.Functions fn = new CMS.Functions();
    public StoredProcedureValues Results = new StoredProcedureValues();

    private long? HrsID = 0;
    private int Action;

    protected void Page_Init(object sender, EventArgs e)
    {
        if (CMS.Rights.HRS == false) { Response.Redirect("Forbidden.aspx"); }

        if (!CMS.Functions.VerifyLogin.Valid) { Response.Redirect("Login.aspx"); }
        else
        {
            if (Request.QueryString["HrsID"] != null) { HrsID = Convert.ToInt64(Request.QueryString["HrsID"].ToString()); }            
            if (Request.QueryString["CMD"] != null) { Action = Convert.ToInt16(Request.QueryString["CMD"].ToString()); }

            Session.Add("HrsID", HrsID);
            Session.Add("CMD", Action);

            if (!IsPostBack) { FillValues(); }
        }
    }

    void FillValues()
    {
        if (Action == CMD.View || Action == CMD.Update || Action == CMD.Delete)
        {
            var Hours = CMS.Hours.GetHours(CMS.Functions.AdministrationID, HrsID, CMS.Functions.UserID, null, null, null, null,null,null).FirstOrDefault();

            if (Hours != null && HrsID.HasValue)
            {
                HRS_COMPANYNAME.Text = Hours.HRS_COMPANYNAME;
                HRS_DATE.Text = Hours.HRS_DATE.HasValue ? Hours.HRS_DATE.Value.ToShortDateString() : "";
                HRS_WORKDAY.Text = Hours.HRS_WORKDAY;
                HRS_PAYDAY.Text = Hours.HRS_PAYDAY.Value.ToShortDateString();
                HRS_HOURS.Text = Hours.HRS_HOURS.ToString();
                HRS_COMMENTS.Text = Hours.HRS_COMMENTS;

            }
        }

        if (Action == CMD.Delete)
        {
            DisableControls.DisableControl(this.Form);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if(Action == CMD.Update)
        {
              BtnSave.Text ="Edit";
        }
        else if(Action == CMD.Delete)
        {
              BtnSave.Text ="Delete";
        }
    }

    protected void BtnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Hours.aspx");
    }

    void HandleErrorMessages()
    {
        ((Content)Master).Jscript =
        String.IsNullOrEmpty(HRS_COMPANYNAME.Text) ? CMS.Functions.NormalPopup("Please enter Company name !",
        System.Drawing.KnownColor.Red,
        " $('.Company').css('border', '1px solid White').css('color','White').css('background-color','#f59e13');") :
        String.IsNullOrEmpty(HRS_WORKDAY.Text) ? CMS.Functions.NormalPopup("Please enter Work day !",
        System.Drawing.KnownColor.Red,
        " $('.WorkDay').css('border', '1px solid White').css('color','White').css('background-color','#f59e13');") :
        String.IsNullOrEmpty(HRS_HOURS.Text) ? CMS.Functions.NormalPopup("Please enter Hours !",
        System.Drawing.KnownColor.Red,
        " $('.Hours').css('border', '1px solid White').css('color','White').css('background-color','#f59e13');") : null;
    }

    void Values()
    {
        if (Action == CMD.Update)
        {
            Results = CMS.StoredProcedureValues.SP_Hours(CMD.Update, CMS.Functions.AdministrationID, HrsID, CMS.Functions.UserID, HRS_COMPANYNAME.Text, Convert.ToDateTime(HRS_DATE.Text), HRS_WORKDAY.SelectedItem.Text, Convert.ToDateTime(HRS_PAYDAY.Text), Convert.ToInt64(HRS_HOURS.Text), HRS_COMMENTS.Text);

            if (Results.Error == 0)
            {
                ((Content)Master).Jscript = CMS.Functions.ConfirmPopup("Updated succesfully !", "Hours.aspx", System.Drawing.KnownColor.Black);
            }
            else
            {
                ((Content)Master).Jscript = CMS.Functions.NormalPopup(Results.Message, System.Drawing.KnownColor.Red);
            }

        }

        if (Action == CMD.Delete)
        {
            Results = CMS.StoredProcedureValues.SP_Hours(CMD.Delete, CMS.Functions.AdministrationID, HrsID, CMS.Functions.UserID, HRS_COMPANYNAME.Text, Convert.ToDateTime(HRS_DATE.Text), HRS_WORKDAY.SelectedItem.Text, Convert.ToDateTime(HRS_PAYDAY.Text), Convert.ToInt64(HRS_HOURS.Text), HRS_COMMENTS.Text);

            if (Results.Error == 0)
            {
                ((Content)Master).Jscript = CMS.Functions.ConfirmPopup("Deleted succesfully !", "Hours.aspx", System.Drawing.KnownColor.Black);
            }
            else
            {
                ((Content)Master).Jscript = CMS.Functions.NormalPopup(Results.Message, System.Drawing.KnownColor.Red);
            }

        }

        if (Action == CMD.Insert)
        {
            Results = CMS.StoredProcedureValues.SP_Hours(CMD.Insert, CMS.Functions.AdministrationID, null, CMS.Functions.UserID, HRS_COMPANYNAME.Text, Convert.ToDateTime(HRS_DATE.Text), HRS_WORKDAY.SelectedItem.Text, Convert.ToDateTime(HRS_PAYDAY.Text), Convert.ToInt64(HRS_HOURS.Text), HRS_COMMENTS.Text);

                if (Results.Error == 0)
                {
                    ((Content)Master).Jscript = CMS.Functions.ConfirmPopup("Inserted succesfully !", "Hours.aspx", System.Drawing.KnownColor.Black);
                }
                else
                {
                    ((Content)Master).Jscript = CMS.Functions.NormalPopup(Results.Message, System.Drawing.KnownColor.Red);
                }


        }
    }

    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        if (Action != CMD.Delete)
        {
            if (!String.IsNullOrEmpty(HRS_COMPANYNAME.Text) && !String.IsNullOrEmpty(HRS_WORKDAY.SelectedItem.Text) && !String.IsNullOrEmpty(HRS_HOURS.Text))
            {
                Values();
            }
            else
            {
                HandleErrorMessages();
            }
        }
        else
        {
            Values();
        }

    }
}